home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / texture / wrap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.6 KB  |  230 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* wrap.c
  19.  * This program reads in a single image from a .rgb file, and 
  20.  * creates and load mipmaps of the image into texture memory. It
  21.  * sets the texture up to clamp in the s dimension and repeat in the 
  22.  * t dimension, and then applies the texture to a rectangular
  23.  * polygon using explicit texture coordinates outside the range
  24.  * [0,1].
  25.  *
  26.  *    <a> key        - toggle animation on/off
  27.  *    Escape Key    - exit program
  28.  */
  29. #include <GL/gl.h>
  30. #include <GL/glu.h>
  31. #include <GL/glut.h>
  32.  
  33. #include <math.h>
  34. #include <stdio.h>
  35.  
  36. #include "rgbImageFile.h"    /* should be in ../../include */
  37.  
  38. /*  Function Prototypes  */
  39.  
  40. GLvoid  initgfx( GLvoid );
  41. GLvoid  animate( GLvoid );
  42. GLvoid  visibility( GLint );
  43. GLvoid  drawScene( GLvoid );
  44. GLvoid  reshape( GLsizei, GLsizei );
  45. GLvoid  keyboard( GLubyte, GLint, GLint );
  46.  
  47. GLvoid  initTexture( unsigned int *, GLsizei, GLsizei );
  48.  
  49. void  printHelp( char *progname );
  50.  
  51. /* Global Definitions */
  52.  
  53. #define KEY_ESC    27    /* ascii value for the escape key */
  54.  
  55. /* Global Variables */
  56.  
  57. static GLfloat swim = 1.0;
  58.  
  59. static GLboolean animateFlag = GL_TRUE;
  60.  
  61. void
  62. main ( int argc, char *argv[])
  63. {
  64.     char        *imageFileName = "maxHeadroom.rgb";
  65.     unsigned int     *image;
  66.     GLsizei        width, height;
  67.     GLsizei        imageWidth, imageHeight;
  68.     
  69.     glutInit( &argc, argv );
  70.  
  71.     if (argc < 2) {
  72.         fprintf (stderr, "usage: %s <imageFileName>\n", argv[0] );
  73.     } else
  74.         imageFileName = argv[1];
  75.  
  76.     fprintf(stdout, "using image %s\n\n", imageFileName );
  77.  
  78.     image = rgbReadImageFile(imageFileName, &imageWidth,
  79.              &imageHeight);
  80.  
  81.     /* create a window that is 1/4 the size of the screen */
  82.  
  83.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  84.     height = glutGet( GLUT_SCREEN_HEIGHT );
  85.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  86.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  87.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
  88.     glutCreateWindow( argv[0] );
  89.  
  90.     initTexture( image, imageWidth, imageHeight );
  91.     initgfx();
  92.  
  93.     glutKeyboardFunc( keyboard );
  94.     glutIdleFunc( animate );
  95.     glutVisibilityFunc( visibility );
  96.     glutReshapeFunc( reshape );
  97.     glutDisplayFunc( drawScene ); 
  98.  
  99.     printHelp( argv[0] );
  100.  
  101.     glutMainLoop();
  102. }
  103.  
  104. GLvoid
  105. printHelp( char *progname )
  106. {
  107.     fprintf(stdout, "\n%s - demonstrates texture wrapping\n"
  108.         "<a> key        - toggle animation on/off\n"
  109.         "Escape key     - exit the program\n\n",
  110.         progname );
  111. }
  112.  
  113. GLvoid initgfx( GLvoid )
  114. {
  115.     glClearColor( 0, 0, 0, 1 );
  116. }
  117.  
  118. GLvoid 
  119. initTexture( unsigned int *image, 
  120.     GLsizei imageWidth, GLsizei imageHeight )
  121. {
  122.     /* use gluBuild2DMipmaps to create and load mipmaps (it will 
  123.      * also scale the original image to be a power of two, if 
  124.      * necessary)
  125.      *
  126.      *      gluBuild2DMipmaps( target, components, width, height,
  127.      *              format,  type, imageArray ) 
  128.      */
  129.     gluBuild2DMipmaps( GL_TEXTURE_2D, 4, imageWidth, imageHeight,
  130.              GL_RGBA, GL_UNSIGNED_BYTE, image );
  131.  
  132.     /* Setting the magnification filter to nearest instead of linear 
  133.      * may run faster on some platforms, with possibly lower quality 
  134.      */
  135.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  136.  
  137.     /* Set the texture up to clamp in the s dimension and 
  138.      * repeat in the t dimension when the texture 
  139.      * coordinates are outside the range [0,1]
  140.      */
  141.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
  142.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  143.  
  144.     /* enable 2D texture mapping */
  145.     glEnable( GL_TEXTURE_2D );
  146. }
  147.  
  148. GLvoid 
  149. keyboard( GLubyte key, GLint x, GLint y )
  150. {
  151.     switch (key) {
  152.     case 'a':    /* toggle animation */
  153.         animateFlag = !animateFlag;
  154.         if (animateFlag)
  155.             glutIdleFunc( animate );
  156.         else
  157.             glutIdleFunc( NULL );
  158.         break;
  159.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  160.         exit(0);
  161.     }
  162. }
  163.  
  164. GLvoid 
  165. animate( GLvoid )
  166. {
  167.     swim = fmodf( swim + 0.1, 35.0 );
  168.  
  169.     /* Tell GLUT to redraw the scene */
  170.     glutPostRedisplay();
  171. }
  172.  
  173. GLvoid
  174. visibility( int state ) 
  175. {
  176.     if (state == GLUT_VISIBLE && animateFlag) {
  177.         glutIdleFunc( animate );
  178.     } else {
  179.         glutIdleFunc( NULL );
  180.     }
  181. }
  182.  
  183. GLvoid
  184. reshape( GLsizei width, GLsizei height )
  185. {
  186.     GLdouble      aspect;
  187.  
  188.     glViewport( 0, 0, width, height );
  189.  
  190.     aspect = (GLdouble) width / (GLdouble) height;
  191.  
  192.     glMatrixMode( GL_PROJECTION );
  193.     glLoadIdentity();
  194.     gluPerspective( 45.0, aspect, 1.0, 50.0 );
  195.     glMatrixMode( GL_MODELVIEW );
  196.     glLoadIdentity();
  197.     glTranslatef( 0.0, 0.0, -12.0 ); 
  198. }
  199.  
  200. GLvoid
  201. drawScene(void)
  202. {
  203.     static float v0[3] = { -1.5, -1.0, 0.0 };
  204.     static float v1[3] = {  1.5, -1.0, 0.0 };
  205.     static float v2[3] = {  1.5,  1.0, 0.0 };
  206.     static float v3[3] = { -1.5,  1.0, 0.0 };
  207.  
  208.     static float t0[2] = { 0.0, 0.0 };
  209.     static float t1[2] = { 2.0, 0.0 };
  210.     static float t2[2] = { 2.0, 2.0 };
  211.     static float t3[2] = { 0.0, 2.0 };
  212.  
  213.     glClear( GL_COLOR_BUFFER_BIT );
  214.  
  215.     glColor4f( 1.0, 1.0, 1.0, 1.0 );
  216.     glPushMatrix ();
  217.         glTranslatef( -6.0 + swim, 0.2*sinf(swim * 3.0), -swim );
  218.     
  219.         glBegin( GL_QUADS );
  220.             glTexCoord2fv( t0 ); glVertex3fv( v0 );
  221.             glTexCoord2fv( t1 ); glVertex3fv( v1 );
  222.             glTexCoord2fv( t2 ); glVertex3fv( v2 );
  223.             glTexCoord2fv( t3 ); glVertex3fv( v3 );
  224.         glEnd();
  225.  
  226.     glPopMatrix ();
  227.  
  228.     glutSwapBuffers();
  229. }
  230.